//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
//import android.util.Log;


// Class for reading CPU information
public class ReadCurrent {
	
	//private static final String TAG = "Tokaplot";
	
	int fType;
	
	String fNames[] = {
			"//sys//class//power_supply//battery//batt_current",														// HTC desire / HD / z
			"//sys//class//power_supply//battery//current_now",															// HTC
			"//sys//devices//platform//ds2784-battery//getcurrent",														// N1 CM
			"//sys//class//power_supply//battery//current_now", 														// N1
			"//sys//devices//platform//i2c-adapter//i2c-0//0-0036//power_supply//ds2746-battery//current_now", 			// Sony X10
			"//sys//devices//platform//i2c-adapter//i2c-0//0-0036//power_supply//battery//current_now", 				// xAndroid
			"//sys//class//power_supply//battery//charger_current", 													// Sony X10
			"//sys//class//power_supply//max17042-0//current_now"														// Nook
	};
	
	boolean fileExists = false;
	
	public ReadCurrent() {
		
		// Try to open file
		File fBatCurrent = null;
		fileExists = false;
		fType = -1;
		try {
			for (fType = 0; fType < fNames.length; fType++) {
				fBatCurrent = new File(fNames[fType]);
				if (fBatCurrent.exists())
					break;
			}
		} catch (Exception e) {
			fBatCurrent = null;
			fType = -1;
		}
		
		if (fBatCurrent!= null) {
			if (!fBatCurrent.exists()) {
				fType = -1;
				fileExists = false;	
			} else {
				fileExists = true;
			}
		}
	}
	
	public boolean canRead() {
		return fileExists;
	}
	
	public boolean read(float [] cur_val) {
		
 		// Battery current
    	if (fType >= 0) {
    		
    		File fBatCurrent = new File(fNames[fType]);
    		if (fBatCurrent.exists()) {
	    			
    			// These file types have a single value in a file
    			try {
    				//Read file
    				String line;
    				BufferedReader brBat = new BufferedReader(new FileReader(fBatCurrent), 20);
    				if ((line = brBat.readLine()) != null) {
    					int btCurrent = Math.abs(Integer.parseInt(line)); // Gets the current (micro A)
    					cur_val[0] = (float)btCurrent;
    					brBat.close();
    					return true;
    				}

    				// Can't find current values
    				brBat.close();
    			}
    			catch (Exception e) {

    				// Exception reading file
    				return false;
    			}
	    	}

    	}
    	return false;
	}
	
}
